<?php

namespace app\admin\controller\fssy;

use app\common\controller\Backend;
use think\Log;

/**
 * 护士季报-职称人数
 *
 * @icon fa fa-circle-o
 */
class Nsqtrt6jt extends Backend
{

    /**
     * Nsqtrt6jt模型对象
     * @var \app\admin\model\fssy\Nsqtrt6jt
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fssy\Nsqtrt6jt;
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        $this->model = new \app\admin\model\fssy\Nurseinfo;
        // 获取最小的 updatetime 数据
        $min_updatetime_data = $this->model->order('updatetime', 'asc')->limit(1)->find();
        // 获取最大的 updatetime 数据
        $max_updatetime_data = $this->model->order('updatetime', 'desc')->limit(1)->find();

        // 时间戳转换成时间 返回2022-05-10 12:51:18格式
        $min_updatetime = date('Y-m-d H:i:s', $min_updatetime_data['updatetime']);
        // 时间戳转换成时间 返回2024-12-07 16:01:29格式
        $max_updatetime = date('Y-m-d H:i:s', $max_updatetime_data['updatetime']);

        // 最小 取前4位字符 年 = 2022
        $min_updatetime_year = substr($min_updatetime, 0, 4);
        // 最小 取前5位和6位字符 月 = 05
        $min_updatetime_month = substr($min_updatetime, 5, 2);
        // 最大 取前4位字符 2024
        $max_updatetime_year = substr($max_updatetime, 0, 4);
        // 最大 取前5位和6位字符 月 = 12
        $max_updatetime_month = substr($max_updatetime, 5, 2);

        // 0:"2022-01-01 ~ 2022-03-31 一季度"
        // 1:"2022-04-01 ~ 2022-06-30 二季度"
        // 2:"2022-07-01 ~ 2022-09-30 三季度"
        // 3:"2022-10-01 ~ 2022-12-31 四季度"
        // 4:"2023-01-01 ~ 2023-03-31 一季度"
        // 5:"2023-04-01 ~ 2023-06-30 二季度"
        // 6:"2023-07-01 ~ 2023-09-30 三季度"
        // 7:"2023-10-01 ~ 2023-12-31 四季度"
        // 8:"2024-01-01 ~ 2024-03-31 一季度"
        // 9:"2024-04-01 ~ 2024-06-30 二季度"
        // 10:"2024-07-01 ~ 2024-09-30 三季度"
        // 11:"2024-10-01 ~ 2024-12-31 四季度"

        // @@@  $max_updatetime_month = 2022; $min_updatetime_month = 05; $max_updatetime_year = 2024; $max_updatetime_month = 12;
        $quarterArr = $this->generateQuarterDates($min_updatetime_year, $min_updatetime_month, $max_updatetime_year, $max_updatetime_month);

        /**
         *  [
         *   "2022-04-01 00:00:00~2022-06-30 23:59:59",
         *   "2022-07-01 00:00:00~2022-09-30 23:59:59",
         *   "2022-10-01 00:00:00~2022-12-31 23:59:59",
         *   "2024-01-01 00:00:00~2023-03-31 23:59:59",
         *   "2024-04-01 00:00:00~2023-06-30 23:59:59",
         *   "2024-07-01 00:00:00~2023-09-30 23:59:59",
         *   "2024-10-01 00:00:00~2023-12-31 23:59:59",
         *   "2024-01-01 00:00:00~2024-03-31 23:59:59",
         *   "2024-04-01 00:00:00~2024-06-30 23:59:59",
         *   "2024-07-01 00:00:00~2024-09-30 23:59:59",
         *   "2024-10-01 00:00:00~2024-12-31 23:59:59"
         *  ]
         */
        $dateTimeArr = $this->convertQuarterDates($quarterArr);

        /***
         * [
                { "start": 1648742400, "end": 1656604799 },
                { "start": 1656604800, "end": 1664553599 },
                { "start": 1664553600, "end": 1672502399 },
                { "start": 1704038400, "end": 1680278399 },
                { "start": 1711900800, "end": 1688140799 },
                { "start": 1719763200, "end": 1696089599 },
                { "start": 1727712000, "end": 1704038399 },
                { "start": 1704038400, "end": 1711900799 },
                { "start": 1711900800, "end": 1719763199 },
                { "start": 1719763200, "end": 1727711999 },
                { "start": 1727712000, "end": 1735660799 }
            ]
         */
        $dateStampsArr = $this->dateTimeArrToTimestamps($dateTimeArr);

        Log::record('$dateTimeArr = ' . json_encode($dateTimeArr));
        Log::record('$dateStampsArr = ' . json_encode($dateStampsArr));
        // $data = $this->model->where('')->select();

        // 遍历 $dateStampsArr，进行批量查询
        foreach ($dateStampsArr as $item) {
            $dataArr[] = $this->model
                ->with(["dictsjobtitle"])
                ->whereTime('fa_fssy_nurseinfo.updatetime', '>=', $item["start"])
                ->whereTime('fa_fssy_nurseinfo.updatetime', '<=', $item["end"])
                ->select();
        }

        Log::record('$dataArr = ' . json_encode($dataArr));

        // [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        // $list = $this->model
        //     ->where($where)
        //     ->order($sort, $order)
        //     ->paginate($limit);
        // $result = ['total' => $list->total(), 'rows' => $list->items()];
        // return json($result);
    }

    /**
     * 生成指定年份的季度信息
     *Str
     * @param [Str] $min_year   "2022"
     * @param [Str] $min_month  "01"
     * @param [Str] $max_year   "2024"
     * @param [Str] $max_month  "12"
     * @return  // 
     * 0:  "2022-01-01 ~ 2022-03-31 一季度"
     * 1:  "2022-04-01 ~ 2022-06-30 二季度"
     * 2:  "2022-07-01 ~ 2022-09-30 三季度"
     * 3:  "2022-10-01 ~ 2022-12-31 四季度"
     * 4:  "2023-01-01 ~ 2023-03-31 一季度"
     * 5:  "2023-04-01 ~ 2023-06-30 二季度"
     * 6:  "2023-07-01 ~ 2023-09-30 三季度"
     * 7:  "2023-10-01 ~ 2023-12-31 四季度"
     * 8:  "2024-01-01 ~ 2024-03-31 一季度"
     * 9:  "2024-04-01 ~ 2024-06-30 二季度"
     * 10  :"2024-07-01 ~ 2024-09-30 三季度"
     * 11  :"2024-10-01 ~ 2024-12-31 四季度"
     */
    protected function generateQuarterDates($min_year, $min_month, $max_year, $max_month)
    {
        // 中文季度映射
        $quarter_names = ["一季度", "二季度", "三季度", "四季度"];
        $quarters = [];

        // 定义固定的季度区间
        $quarter_dates = [
            "01-01 ~ 03-31", // 一季度
            "04-01 ~ 06-30", // 二季度
            "07-01 ~ 09-30", // 三季度
            "10-01 ~ 12-31"  // 四季度
        ];

        // 计算从哪个季度开始
        $start_quarter = ceil((int)$min_month / 3);  // 计算从哪个季度开始
        // 计算到哪个季度结束
        $end_quarter = ceil((int)$max_month / 3);  // 计算到哪个季度结束

        // 遍历从最小年份到最大年份的每个年份
        for ($year = $min_year; $year <= $max_year; $year++) {
            // 如果是最小年份，开始季度需要从 $start_quarter 开始
            if ($year == $min_year) {
                $start = $start_quarter - 1;  // 起始季度的索引
            } else {
                $start = 0;  // 其他年份从一季度开始
            }

            // 如果是最大年份，结束季度需要到 $end_quarter 结束
            if ($year == $max_year) {
                $end = $end_quarter - 1;  // 结束季度的索引
            } else {
                $end = 3;  // 其他年份直到四季度
            }

            // 遍历每个季度
            for ($quarter = $start; $quarter <= $end; $quarter++) {
                // 获取季度名称
                $quarter_name = $quarter_names[$quarter];

                // 计算季度的开始日期和结束日期
                $start_date = "$year-" . substr($quarter_dates[$quarter], 0, 5); // 获取开始日期
                $end_date = "$year-" . substr($quarter_dates[$quarter], strpos($quarter_dates[$quarter], '~') + 1); // 获取结束日期
                $end_date = str_replace(" ", "", $end_date);
                // 将季度信息添加到数组中
                $quarters[] = "$start_date ~ $end_date $quarter_name";
            }
        }

        return $quarters;
    }

    /**
     * 将中文季度映射为日期时间
     *
     * @param [Arr] generateQuarterDates 返回值
     * @return 
     *  *  [
     *   "2022-04-01 00:00:00~2022-06-30 23:59:59",
     *   "2022-07-01 00:00:00~2022-09-30 23:59:59",
     *   "2022-10-01 00:00:00~2022-12-31 23:59:59",
     *   "2024-01-01 00:00:00~2023-03-31 23:59:59",
     *   "2024-04-01 00:00:00~2023-06-30 23:59:59",
     *   "2024-07-01 00:00:00~2023-09-30 23:59:59",
     *   "2024-10-01 00:00:00~2023-12-31 23:59:59",
     *   "2024-01-01 00:00:00~2024-03-31 23:59:59",
     *   "2024-04-01 00:00:00~2024-06-30 23:59:59",
     *   "2024-07-01 00:00:00~2024-09-30 23:59:59",
     *   "2024-10-01 00:00:00~2024-12-31 23:59:59"
     *  ]
     */
    protected function convertQuarterDates($inputDates)
    {
        $outputDates = [];
        foreach ($inputDates as $dateRange) {
            // 使用正则表达式提取日期部分
            preg_match('/(\d{4}-\d{2}-\d{2}) ~ (\d{4}-\d{2}-\d{2})/', $dateRange, $matches);
            if (count($matches) === 3) {
                $startDate = $matches[1];
                $endDate = $matches[2];

                // 将结束日期设置为当天的最后一秒
                $endDate = date('Y-m-d 23:59:59', strtotime($endDate));

                // 如果开始日期是2022年，则保持不变；否则修改为2024年
                if (strpos($startDate, '2022') === 0) {
                    $newStartDate = $startDate;
                } else {
                    $newStartDate = '2024-' . substr($startDate, 5);
                }

                // 格式化新的日期范围字符串
                $newDateRange = $newStartDate . ' 00:00:00~' . $endDate;
                $outputDates[] = $newDateRange;
            }
        }
        return $outputDates;
    }

    /**
     * 将日期时间数组转换为时间戳数组
     *
     * @param [Arr] convertQuarterDates 返回值
     * @return 
     * 0:  "2022-01-01 ~ 2022-03-31 一季度"
     * 1:  "2022-04-01 ~ 2022-06-30 二季度"
     * 2:  "2022-07-01 ~ 2022-09-30 三季度"
     * 3:  "2022-10-01 ~ 2022-12-31 四季度"
     * 4:  "2023-01-01 ~ 2023-03-31 一季度"
     * 5:  "2023-04-01 ~ 2023-06-30 二季度"
     * 6:  "2023-07-01 ~ 2023-09-30 三季度"
     * 7:  "2023-10-01 ~ 2023-12-31 四季度"
     * 8:  "2024-01-01 ~ 2024-03-31 一季度"
     * 9:  "2024-04-01 ~ 2024-06-30 二季度"
     * 10  :"2024-07-01 ~ 2024-09-30 三季度"
     * 11  :"2024-10-01 ~ 2024-12-31 四季度"
     */
    protected function dateTimeArrToTimestamps($dateTimeArr)
    {
        // 设置默认时区为“PRC”
        date_default_timezone_set("PRC");

        $timestampArr = [];

        foreach ($dateTimeArr as $dateTimeRange) {
            // 使用 "~" 分割开始时间和结束时间
            list($startDateTime, $endDateTime) = explode('~', $dateTimeRange);

            // 将开始时间和结束时间转换为时间戳
            $startTimestamp = strtotime($startDateTime);
            $endTimestamp = strtotime($endDateTime);

            // 将时间戳添加到结果数组中
            $timestampArr[] = [
                'start' => $startTimestamp,
                'end' => $endTimestamp
            ];
        }

        return $timestampArr;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
}
